package com.cytech.cloudclinic.moudle.main.clinic.admission.intelligent;

import android.Manifest;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.cytech.cloudclinic.R;
import com.cytech.cloudclinic.adapter.IllnessAdapter;
import com.cytech.cloudclinic.base.BaseFragment;
import com.cytech.cloudclinic.base.BasePresenter;
import com.cytech.cloudclinic.bean.Illness;
import com.cytech.cloudclinic.bean.LoadImageEntity;
import com.cytech.cloudclinic.bean.UploadImage;
import com.cytech.cloudclinic.moudle.main.clinic.ClinicFragment;
import com.cytech.cloudclinic.moudle.main.clinic.admission.report.ReportRecordFragment;
import com.cytech.cloudclinic.moudle.main.clinic.result.IntelligentResultActivity;
import com.cytech.cloudclinic.utils.GetPathFromUri;
import com.cytech.cloudclinic.utils.MatisseUtils;
import com.cytech.cloudclinic.view.SelectImageView;
import com.nabinbhandari.android.permissions.PermissionHandler;
import com.nabinbhandari.android.permissions.Permissions;
import com.zhihu.matisse.Matisse;
import com.zxy.tiny.Tiny;
import com.zxy.tiny.callback.FileCallback;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

import static android.app.Activity.RESULT_OK;

/**
 * 智能诊疗服务
 */
public class IntelligentFragment extends BaseFragment<IntelligentPresenter> implements IntelligentView,SelectImageView.OnItemImageSelectListener {
    @BindView(R.id.rv_intelligent_disease)
    RecyclerView rvDisease;
    @BindView(R.id.st_select_image_view)
    SelectImageView selectImageView;
    @BindView(R.id.et_confirm_illness)
    EditText etIllness;

    List<Uri> mSelected;

    //选取图片
    private int REQUEST_CODE_CHOOSE = 1001;
    private IllnessAdapter illnessAdapter;
    private List<Illness> illnessesList;


    public static IntelligentFragment newInstance() {
        IntelligentFragment fragment = new IntelligentFragment();
        //如果有参数，使用Bundle传递
        return fragment;
    }

    @Override
    protected IntelligentPresenter createPresenter() {
        return new IntelligentPresenter(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_intelligent_treatment;
    }

    @Override
    protected void initView() {
        selectImageView.setOnItemSelectListener(this);
    }

    @Override
    protected void initData() {
        illnessesList = new ArrayList<>();
        illnessAdapter = new IllnessAdapter(illnessesList);
        rvDisease.setLayoutManager(new GridLayoutManager(getActivity(),3));
        rvDisease.setAdapter(illnessAdapter);
        illnessAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                illnessAdapter.setCurrentPos(position);
                etIllness.setText(illnessAdapter.getItem(position).name);
            }
        });

        presenter.getIllnessList(1);
    }

    @Override
    public void getIllnessListSuccess(List<Illness> data) {
        illnessAdapter.setNewInstance(data);
    }

    @Override
    public void loadSuccess(LoadImageEntity result, Uri uri) {
        Log.d("TAG", "loadSuccess: "+"图片上传成功");
        hideLoading();
        //图片上传成功
        UploadImage uploadImage = new UploadImage();
        uploadImage.localUri = uri;
        uploadImage.url = result.url;
        selectImageView.notifySelectData(uploadImage);
        mSelected.remove(uri);
        if (mSelected.size() > 0) {
            uploadImage(mSelected.get(0));
        }
    }

    @Override
    public void onImageSelectSelect(int seleteTag) {
        String[] permissions = {Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA};
        Permissions.check(getActivity(), permissions, null, null, new PermissionHandler() {
            @Override
            public void onGranted() {
                MatisseUtils.getInstance().openPicker(IntelligentFragment.this, 1, true, REQUEST_CODE_CHOOSE);
            }
        });
    }

    @Override
    public void onImageDelete(int position, int seleteTag) {

    }

    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        //super.onActivityResult(requestCode, resultCode, data);
        Log.d("TAG", requestCode+"onActivityResult: "+resultCode);
        if (resultCode == RESULT_OK) {
            if (requestCode == REQUEST_CODE_CHOOSE) {
                mSelected = Matisse.obtainResult(data);
                if (mSelected != null && mSelected.size() > 0) {
                    uploadImage((mSelected.get(0)));
                }
            }
        }
    }

    public void uploadImage(Uri uri) {
        Tiny.FileCompressOptions options = new Tiny.FileCompressOptions();
        options.size = 100;
        Tiny.getInstance().source(GetPathFromUri.getPath(getActivity(), uri)).asFile().withOptions(options).compress(new FileCallback() {
            @Override
            public void callback(boolean isSuccess, String outfile, Throwable t) {
                Log.d("TAG", "callback: " + new File(outfile).length());
                presenter.uploadImage(getActivity(), outfile, uri);
            }
        });
    }

    @OnClick(R.id.btn_intelligent_teatment)
    public void intelligentTreatment(View view){
        IntelligentResultActivity.actionStart(getActivity());
    }
}
